//
//  NWStatusPhotosView.swift
//  Weibo
//
//  Created by Northwan on 16/1/27.
//  Copyright © 2016年 cn.northwan. All rights reserved.
//

import UIKit

//间距
private let itemMargin:CGFloat = 5
//每个条目的宽高
private let itemWH = (SCREENW - 2 * itemMargin - 2 * NWStatusCellSubViewsMargin) / 3

private let NWStatusPhotosViewCellIdentifier = "NWStatusPhotosViewCellIdentifier"

class NWStatusPhotosView: UICollectionView {

    var pic_urls:[NWStatusPhoto]?{
        didSet{
            contentLabel.text = String(pic_urls?.count ?? 0)
            
            let size = calcSize(pic_urls?.count ?? 0)
            
            snp_updateConstraints { (make) -> Void in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
        registerClass(NWStatusPhotoCell.self, forCellWithReuseIdentifier: NWStatusPhotosViewCellIdentifier)
        
        dataSource = self
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        //设置列间距
        layout.minimumInteritemSpacing = itemMargin
        //设置行间距
        layout.minimumLineSpacing = itemMargin
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = RandomColor()
        addSubview(contentLabel)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    /**
     根据图片个数计算当前View的Size
     
     - parameter count: 图片个数
     
     - returns:
     */
    private func calcSize(count:Int)->CGSize{
        
        
        //列
        let cols = count == 4 ? 2 : (count >= 3) ? 3 : count
        //行
        let rows = count == 4 ? 2 : (count - 1) / 3 + 1
        
        
        //计算宽度和高度
        let width = CGFloat(cols) * itemWH + (CGFloat(cols) - 1) * itemMargin
        let height = CGFloat(rows) * itemWH + (CGFloat(rows) - 1) * itemMargin
        
        
        return CGSize(width: width, height: height)
    }
    
    //// MARK: - 懒加载
    private lazy var contentLabel = UILabel(textColor: UIColor.blackColor(), fontSize: 18)
    
}


extension NWStatusPhotosView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NWStatusPhotosViewCellIdentifier, forIndexPath: indexPath) as! NWStatusPhotoCell
        cell.backgroundColor = RandomColor()
        cell.statusPhoto = pic_urls?[indexPath.row]
        return cell
    }
}


class NWStatusPhotoCell: UICollectionViewCell {
    
    var statusPhoto:NWStatusPhoto?{
        didSet{
            if let urlString = statusPhoto?.thumbnail_pic {
                
                gifImageView.hidden = !urlString.hasSuffix(".gif")
                
                if let url =  NSURL(string: urlString){
                    self.imageView.setImageWithURL(url, placeholderImage: UIImage(named: "timeline_image_gif"))
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imageView)
        
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self.snp_trailing)
            make.bottom.equalTo(self.snp_bottom)
        }
    }
    
    // MARK: - 懒加载
    private lazy var imageView:UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var gifImageView:UIImageView = {
        let gifView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        return gifView
    }()
    
}
